//获取所有读者信息
window.onload = function () {

  //发送ajax
  axios({
    url: 'http://localhost:8080/admin/reader_info/queryAll',
    method: 'post',
  })
    .then(res => {
      if (res.data.code == 200) {
        // sessionStorage.setItem('reader_info',JSON.stringify(res.data.data))
        fun(res.data.data)

      }
    })
}
function fun(data) {
  let tbd = document.querySelector('.tbd')
  tbd.innerHTML = ''
  data.forEach((item, index) => {
    const str = `
           <tr>
          <td>${index + 1}</td>
          <td>${item.reader_name}</td>
          <td>${item.sex}</td>
          <td>${item.telephone}</td>
          <td>${item.card}</td>
          <td>
          <button class="btn btn-danger btn-sm" data-id=${item.reader_id}>删除</button>
          <button class="btn btn-warning btn-sm" data-id=${item.reader_id} data-bs-toggle="modal" data-bs-target="#updatestudent">修改</button>
          </td>
</tr>`
    tbd.innerHTML += str
  })
}
//添加读者信息
function AddStudent() {
  //获取数据
  const reader_name = document.getElementById('reader_name').value.trim();
  // const reader_id=document.getElementById('reader_id')
  const sex = document.getElementById('sex').value.trim();
  const telephone = document.getElementById('phone').value.trim();
  const card = document.getElementById('card').value.trim();
  axios({
    url: 'http://localhost:8080/admin/reader_info/add',
    method: 'post',
    data: {
      reader_name, sex, telephone, card
    }
  })
    .then(res => {
      location.reload()
    })
}


//删除读者信息
let tbd = document.querySelector('.tbd')
tbd.addEventListener('click', (e) => {
  if (e.target.className.includes('danger')) {
    const res = window.confirm('确定删除吗？')

    if (res) {
      const idd = e.target.dataset.id
      console.log(idd);
      axios({
        url: 'http://localhost:8080/admin/reader_info/del',
        method: 'post',
        data: {
          idd
        }

      })
        .then(res => {
          location.reload()
        })
    }
  }
  //修改读者信息
  if (e.target.className.includes("warning")) {
    const reader_id = e.target.dataset.id
    axios({
      url: 'http://localhost:8080/admin/reader_info/queryOne',
      method: 'post',
      data: { reader_id }


    })
      .then(res => {
        //将服务器响应数据渲染到模态框里面去
        document.getElementById('reader_id').value = res.data.data.reader_id;
        document.getElementById('name_id').value = res.data.data.reader_name;
        document.getElementById('sex_id').value = res.data.data.sex;
        document.getElementById('phone_id').value = res.data.data.telephone;
        document.getElementById('card_id').value = res.data.data.card;
      })
}
  
})
 //修改读者信息/*  */
 function update(){
  const did = document.getElementById('reader_id').value
  const nameid = document.getElementById('name_id').value
  const sexid = document.getElementById('sex_id').value
  const phoneid = document.getElementById('phone_id').value
  const cardid = document.getElementById('card_id').value
  console.log(did);
 axios({
   url:'http://localhost:8080/admin/reader_info/update',
   method:'post',
   data:{did,nameid,sexid,phoneid,cardid}
 })
 .then(res=>{
 
  location.reload()
 })
 }
//根据读者性别查询
function queryByGender(sex){
  axios({
    url:"http://localhost:8080/admin/reader_info/queryByGender",
    data:{sex},
    method:'post'
  })
  .then(res=>{
    console.log(res.data);
    // sessionStorage.setItem('reader_info',JSON.stringify(res.data.data))
    fun(res.data.data)
    
  })
} 

//模糊查询
function queryByLikeName(){
  const reader_name=document.getElementById('likename').value.trim()
  axios({
    url:"http://localhost:8080/admin/reader_info/likename",
    method:'post',
    data:{reader_name}

  })
  .then(res=>{
    fun(res.data.data)
     
  })
}



